<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<u-form :model="infoObj" :errorType="['toast']" label-position="left" label-width="150" label-align="left">
			<u-form-item v-if="judgeShow('te001')" label="采购单别">
				<u-input v-model="infoObj.te001" placeholder="请输入采购单别" disabled></u-input>
			</u-form-item>

			<u-form-item v-if="judgeShow('te002')" label="采购单号">
				<u-input v-model="infoObj.te002" placeholder="请输入采购单号" disabled></u-input>
			</u-form-item>

			<u-form-item v-if="judgeShow('te004')" label="变更日期">
				<u-input v-model="infoObj.te004" placeholder="请输入变更日期" disabled></u-input>
			</u-form-item>

			<u-form-item v-if="judgeShow('te003')" label="版本">
				<u-input v-model="infoObj.te003" placeholder="请输入版本" disabled></u-input>
			</u-form-item>

			<u-form-item v-if="judgeShow('tm001')" label="整张结束">
				<u-switch v-model="value13" disabled></u-switch>
			</u-form-item>

			<u-form-item v-if="judgeShow('te005')" label="供应商编号">
				<u-input v-model="buyName" placeholder="请输入供应商编号" disabled></u-input>
			</u-form-item>

			<u-form-item v-if="judgeShow('te006')" label="变更原因">
				<u-input v-model="infoObj.te006"  type="textarea" placeholder="请输入变更原因" disabled></u-input>
			</u-form-item>

			<view class="jnpf-table" style="margin-top: 24rpx;">
				<u-form-item label="状态">
					单头变更前
				</u-form-item>
				<u-form-item v-if="judgeShow('te024')" label="定金比率">
					<u-input v-model="infoObj.te024" placeholder="请输入定金比率" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te040')" label="采购人员">
					<u-input v-model="userName" placeholder="请输入采购人员" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te039')" label="工厂">
					<u-input v-model="goChangeName" placeholder="请输入工厂" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te108')" label="汇率">
					<u-input v-model="infoObj.te108" placeholder="请输入汇率" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te007')" label="交易币种">
					<u-input v-model="moneyName" placeholder="请输入交易币种" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te023')" label="付款条件">
					<u-input v-model="moneyTypeName" placeholder="请输入付款条件" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te118')" label="税种">
					<u-input v-model="categoriesOfTaxesName" placeholder="请输入税种" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te022')" label="税率">
					<u-input v-model="infoObj.te022" placeholder="请输入税率" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te034')" label="备注">
					<u-input v-model="infoObj.te034" type="textarea" placeholder="请输入备注" disabled></u-input>
				</u-form-item>
			</view>

			<view class="jnpf-table" style="margin-top: 24rpx;">
				<u-form-item   label="状态">
					<view>单头变更后</view>
				</u-form-item>
				<u-form-item v-if="judgeShow('te024')" label="定金比率">
					<u-input v-model="infoObj.te024" placeholder="请输入定金比率" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te040')" label="采购人员">
					<u-input v-model="userName" placeholder="请输入采购人员" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te039')" label="工厂">
					<u-input v-model="goChangeName" placeholder="请输入工厂" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te108')" label="汇率">
					<u-input v-model="infoObj.te108" placeholder="请输入汇率" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te007')" label="交易币种">
					<u-input v-model="moneyName" placeholder="请输入交易币种" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te023')" label="付款条件">
					<u-input v-model="moneyTypeName" placeholder="请输入付款条件" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te118')" label="税种">
					<u-input v-model="categoriesOfTaxesName" placeholder="请输入税种" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te022')" label="税率">
					<u-input v-model="infoObj.te022" placeholder="请输入税率" disabled></u-input>
				</u-form-item>
				<u-form-item v-if="judgeShow('te034')" label="备注">
					<u-input v-model="infoObj.te034" type="textarea" placeholder="请输入备注" disabled></u-input>
				</u-form-item>
			</view>

			<view class="jnpf-table" style="margin-top: 24rpx;">
				<view class="jnpf-table-item" style="margin-bottom: 24rpx;" v-for="(lstItem,i) in itemLst" :key="i">
					<u-form-item   label="序号">
						1
					</u-form-item>
					<u-form-item v-if="judgeShow('tf005')" label="原品号">
						<u-input v-model="lstItem.tf005" placeholder="请输入原品号" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf006')" label="原品名">
						<u-input v-model="lstItem.tf006" placeholder="请输入原品名" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf007')" label="原规格">
						<u-input v-model="lstItem.tf007" placeholder="请输入原规格" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf008')" label="原交货仓库">
						{{cangkuName}}
					</u-form-item>
					<u-form-item v-if="judgeShow('tf009')" label="原采购数量">
						<u-input v-model="lstItem.tf009" placeholder="请输入原采购数量" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf010')" label="原单位">
						<u-input v-model="lstItem.tf010" placeholder="请输入原单位" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf011')" label="原单价">
						<u-input v-model="lstItem.tf011" placeholder="请输入原单价" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf012')" label="原金额">
						<u-input v-model="lstItem.tf012" placeholder="请输入原金额" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf013')" label="原交货日">
						<u-input v-model="lstItem.tf013" placeholder="请输入原交货日" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf015')" label="原赠品">
						<u-input v-model="lstItem.tf015" placeholder="请输入原赠品" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf021')" label="原结束">
						<u-input v-model="lstItem.tf021" placeholder="请输入原结束" disabled></u-input>
					</u-form-item>
				</view>
			</view>

			<view class="jnpf-table" style="margin-top: 24rpx;">
				<view class="jnpf-table-item" style="margin-bottom: 24rpx;" v-for="(item2,i) in purtfList" :key="i">
					<u-form-item   label="序号">
						2
					</u-form-item>
					<u-form-item v-if="judgeShow('tf005')" label="品号">
						<u-input v-model="item2.tf005" placeholder="请输入品号" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf006')" label="品名">
						<u-input v-model="item2.tf006" placeholder="请输入品名" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf007')" label="规格">
						<u-input v-model="item2.tf007" placeholder="请输入规格" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf008')" label="交货仓库">
						{{cangkuName||'无'}}
					</u-form-item>
					<u-form-item v-if="judgeShow('tf009')" label="采购数量">
						<u-input v-model="item2.tf009" placeholder="请输入采购数量" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf010')" label="单位">
						<u-input v-model="item2.tf010" placeholder="请输入单位" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf011')" label="单价">
						<u-input v-model="item2.tf011" placeholder="请输入单价" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf012')" label="金额">
						<u-input v-model="item2.tf012" placeholder="请输入金额" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf013')" label="交货日">
						<u-input v-model="item2.tf013" placeholder="请输入交货日" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf015')" label="赠品">
						<u-input v-model="item2.tf015" placeholder="请输入赠品" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tf021')" label="结束">
						<u-input v-model="item2.tf021" placeholder="请输入结束" disabled></u-input>
					</u-form-item>
				</view>
			</view>
		</u-form>
	</view>
</template>

<script>
	import comMixin from '../mixin'
	import {
		getGoodsList,
		getCustomerList,
		Info,
		Create,
		Update,
	} from '@/api/apply/order'
	import {
		getPurteInfo
	} from '@/api/workFlow/flowBefore'
	import {
		getDictionaryDataSelector,
		getDataInterfaceDataInfo
	} from '@/api/common'
	export default {
		mixins: [comMixin],
		data() {
			return {
				buyName: '',
				buyNameTwo: '',
				infoObj: {},
				lst: [],
				id: '',
				value13: false,
				dataForm: {},
				purtfList: [],
				itemLst: [],
				userName: '',
				goChangeName: '',
				moneyName: '',
				moneyTypeName: '',
				categoriesOfTaxesName: '',
				cangkuName:'',
			}
		},
		 
		methods: {
			init(data, id) {
				this.id = id
				this.setting = data
				this.getInfoFn(id)
			},
			getInfoFn(data) {
				getPurteInfo(data).then(res => {
					if (res.code == 200) {
						this.infoObj = res.data.output
						this.purtfList = this.infoObj.purtfList
						this.itemLst = res.data.item2
						this.getDictionaryDataSelectorFn()
						this.getDataInterfaceDataInfoMV002(this.infoObj.te005)
						this.getDataInterfaceDataInfoUserName(this.infoObj.te040)
						this.getDataInterfaceDataInfoGoChange(this.infoObj.te039)
						this.getDataInterfaceDataInfoGoMoneyType(this.infoObj.te007)
						this.getDataInterfaceDataInfoGoPayType(this.infoObj.te023)
						this.getCangKu(this.itemLst[0].tf008)
					 
					}
				})
			},
			/**
			 * 查询税种列表
			 */
			getDictionaryDataSelectorFn() {
				getDictionaryDataSelector('411695330707391429').then(res => {
					if (res.code == 200) {
						this.categoriesOfTaxesLst = res.data.list
						this.categoriesOfTaxesLst.forEach(res => {
							if (res.enCode == this.infoObj.te118) {
								this.categoriesOfTaxesName = res.fullName
							}
						})
					}
				})
			},

			getDataInterfaceDataInfoMV002(id) {
				let data = {
					id,
					interfaceId: '382423790757396549',
					dblink: this.id,
					propsValue: 'MA001',
					relationField: 'JoinStr',
				}
				console.log('getDataInterfaceDataInfoMV002 data', data)
				getDataInterfaceDataInfo('382423790757396549', data).then(res => {
					if (res.code == 200) {
						this.buyName = res.data.JoinStr
						console.log('buyName', res)
					}
				})
			},
			getDataInterfaceDataInfoUserName(id) {
				let data = {
					id,
					interfaceId: '445425382452126213',
					dblink: this.id,
					propsValue: 'MV001',
					relationField: 'JoinStr',
				}

				getDataInterfaceDataInfo('445425382452126213', data).then(res => {
					if (res.code == 200) {
						this.userName = res.data.JoinStr

					}
				})
			},
			getDataInterfaceDataInfoGoChange(id) {
				let data = {
					id,
					interfaceId: '362111000289221189',
					dblink: this.id,
					propsValue: 'MB001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('362111000289221189', data).then(res => {
					if (res.code == 200) {
						this.goChangeName = res.data.JoinStr
						console.log('buyNameTwo', res)
					}
				})
			},
			getDataInterfaceDataInfoGoMoneyType(id) {
				let data = {
					id,
					interfaceId: '362115609221143109',
					propsValue: 'MF001',
					dblink: this.id,
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('362115609221143109', data).then(res => {
					if (res.code == 200) {
						this.moneyName = res.data.JoinStr
					}
				})
			},
			getDataInterfaceDataInfoGoPayType(id) {
				let data = {
					id,
					interfaceId: '362115953258928709',
					dblink: this.id,
					propsValue: 'NA002',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('362115953258928709', data).then(res => {
					if (res.code == 200) {
						this.moneyTypeName = res.data.JoinStr
					}
				})
			},
			selfGetInfo() {
				Info(this.setting.id).then(res => {
					this.dataForm = res.data
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson)
					}
				})
			},
			getCangKu(id) {
				let data = {
					id,
					interfaceId: '381218413038545541',
					propsValue: 'MC001',
					dblink: this.id,
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('381218413038545541', data).then(res => {
					if (res.code == 200) {
						this.cangkuName = res.data.JoinStr
					}
				})
			},
			test(id) {
				let data = {
					id,
					interfaceId: '381218413038545541',
					propsValue: 'MC001',
					dblink: this.id,
					relationField: 'JoinStr',
				}

				getDataInterfaceDataInfo('381218413038545541', data).then(res => {
					if (res.code == 200) {
						resolve(res.data.JoinStr)
					} else {
						reject('')
					}
				})

			},
			selfSubmit() {
				this.dataForm.status = this.eventType === 'submit' ? 0 : 1
				if (this.eventType === 'save') return this.selfHandleRequest()
				uni.showModal({
					title: '提示',
					content: '您确定要提交当前流程吗？',
					success: res => {
						if (res.confirm) {
							this.selfHandleRequest()
						}
					}
				})
			},
			selfHandleRequest() {
				if (!this.dataForm.id) delete(this.dataForm.id)
				const formMethod = this.dataForm.id ? Update : Create
				formMethod(this.dataForm).then(res => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit('refresh')
								uni.navigateBack()
							}, 1500)
						}
					})
				})
			},
			initData() {
				getGoodsList().then(res => {
					this.goodsOptions = res.data.list
				})
				getCustomerList().then(res => {
					this.customerOptions = res.data.list
				})
			},
			selfInit() {
				this.addGoods()
				this.addPlan()
			},
			onChange(e) {
				const user = e[0]
				this.dataForm.salesmanName = user.fullName
			},
			addGoods() {
				const item = {
					goodsId: '',
					goodsCode: '',
					goodsName: '',
					specifications: '',
					unit: '',
					qty: '',
					price: '',
					amount: '',
					discount: null,
					cess: null,
					actualPrice: '',
					actualAmount: '',
					description: ''
				}
				this.dataForm.goodsList.push(item)
			},
			delGoods(index) {
				this.dataForm.goodsList.splice(index, 1)
			},
			addPlan() {
				const item = {
					receivableDate: '',
					receivableRate: '',
					receivableMoney: '',
					receivableMode: '',
					abstract: ''
				}
				this.dataForm.collectionPlanList.push(item)
			},
			delPlan(index) {
				this.dataForm.collectionPlanList.splice(index, 1)
			},
			onGoodsChange(e, i) {
				const goods = e[0]
				this.dataForm.goodsList[i] = {
					goodsId: goods.id,
					goodsCode: goods.code,
					goodsName: goods.text,
					specifications: goods.specifications,
					unit: goods.unit,
					qty: 1,
					price: goods.price,
					amount: goods.price,
					discount: 100,
					cess: 0,
					actualPrice: goods.price,
					actualAmount: goods.price,
					description: ''
				}
				this.$forceUpdate()
			},
			onCustomerChange(e) {
				const customer = e[0]
				this.dataForm.customerName = customer.text
			},
			count(row) {
				//金额 = 数量*单价
				row.amount = this.jnpf.toDecimal(parseFloat(row.price) * parseFloat(row.qty))
				//折扣价 = (单价*折扣)
				var discountPrice = row.price * (row.discount / 100);
				//实际单价 = 折扣价 * (1 + (税率 / 100))
				row.actualPrice = this.jnpf.toDecimal(discountPrice * (1 + (row.cess / 100)));
				//实际金额
				row.actualAmount = this.jnpf.toDecimal(parseFloat(row.actualPrice) * parseFloat(row
					.qty))
				this.$forceUpdate()
			}
		}
	}
</script>